"use strict";
const fs = require('fs')
const path = require('path')
const mongodbDatas = require(path.join(__dirname, '../tools/Mongodb-datas.js'))

exports.getLoginPage = (req, res) => {
    res.setHeader('Content-Type', 'text/html; charset=utf-8')
    fs.readFile(path.join(__dirname, '../views/login.html'), (err, data) => {
        res.cookie('name', 'tobi', {path: '/', secure: true});
        console.log('ok accout');
        res.end(data)
    })
}

exports.login = (req, res) => {
    const params = req.body
    console.log(params);
    mongodbDatas.findOne('account', params, (doc) => {
        res.setHeader('Content-Type', 'text/html; charset=utf-8')
        if (doc != null) {
            res.end("<script>window.location.href ='/studentmanager/list'</script>")
        } else {
            res.end("<script>alert('用户或密码错误'),window.location.href ='/account/login'</script>")
        }
    })
}